#include "CreateBaseWidget.h"

#include "Core/MainWindow.h"
#include "QEvent.h"
#include "FeatureShape/HistoryManager.h"
#include "FeatureShape/FeatureShape.h"

CreateBaseWidget::CreateBaseWidget(MainWindow* inMainWindow, ShapeOperation* inOperation, QWidget* parent)
	: QRSWidgetBase(inMainWindow, inOperation, parent)
{
	bIsConfirmed = false;

	m_mainWindow->SetSelectionMode(SM_Vertex);

	connect(m_mainWindow, &MainWindow::OnLeftButtonDown, this, &CreateBaseWidget::onLeftButtonDown);
}

CreateBaseWidget::~CreateBaseWidget()
{
	
}

void CreateBaseWidget::onConfirm()
{
	ConfirmActorCreate();
	close();
}

void CreateBaseWidget::onApply()
{
	/*
	 * 1.确认生成actor
	 * 2.将参数重置 
	*/

	ConfirmActorCreate();
	Reset();
}

void CreateBaseWidget::closeEvent(QCloseEvent* event)
{
	if (!bIsConfirmed)
	{
		m_mainWindow->RemoveActorFromRenderer(cacheActor);
	}
	m_mainWindow->SubWidgetClose(bIsConfirmed);
	disconnect(m_mainWindow, &MainWindow::OnLeftButtonDown, this, &CreateBaseWidget::onLeftButtonDown);
}

void CreateBaseWidget::Reset()
{
	m_operation = nullptr;
	cacheActor = nullptr;
	bIsConfirmed = false;
}

void CreateBaseWidget::onLeftButtonDown()
{
	bool bPickerSuccess = false;
	gp_Pnt PickPos = gp_Pnt();
	m_mainWindow->GetVertexPickerTargetPos(bPickerSuccess, PickPos);
    if (bPickerSuccess)
    {
		OnPickPosition(PickPos);
    }
}

void CreateBaseWidget::ConfirmActorCreate()
{
	m_mainWindow->OnActorConfirmCreate(cacheActor);
	// 生成feature shape
	GenerateFeatureShapeForCacheActor();
	// 更新位置参数
	cacheActor->m_featureShape->_position = cacheActor->m_position;
	bIsConfirmed = true;

	m_mainWindow->GetHistoryManager()->applyOperation(GetOperation());
}
